#ifndef TOOLSMETHODBUILDER_H
#define TOOLSMETHODBUILDER_H

#include "IMethodBuilder.h"
#include <QString>
#include <QJsonObject>
#include <QMap>

/**
 * @brief Builder for tool method configurations
 * 
 * Provides a fluent interface for building tool methods with validation.
 * Ensures that tool methods contain all required fields before building.
* The JSON object created by this class looks like:
 * ======================================
 * | {                                  |
 * |     "name": "string",              |
 * |     "title"?: "string",            |
 * |     "description"?: "string",      |
 * |     "inputSchema": {               |
 * |         "properties"?: {           |
 * |             "[key: string]": {}    |
 * |         },                         |
 * |         "required"?: [],           |
 * |         "type": "object"           |
 * |     },                             |
 * |     "outputSchema"?: {             |
 * |         "properties"?: {           |
 * |             "[key: string]": {}    |
 * |         },                         |
 * |         "required"?: [],           |
 * |         "type": "object"           |
 * |     }                              |
 * | }                                  |
 * ======================================
 */
class ToolsMethodBuilder : public IMethodBuilder
{
public:
    struct Property {
        QString name;
        QString type;
        QString description;
        bool isRequired;

        explicit Property() {}
        Property(const QString& _name, const QString& _type, const QString& _description, bool _isRequired = true)
            : name(_name), type(_type), description(_description), isRequired(_isRequired) {}
    };

public:
    ToolsMethodBuilder();
    ~ToolsMethodBuilder() override = default;

public:// IMethodBuilder
    IMethodBuilder& setName(const QString& name) override;
    IMethodBuilder& setTitle(const QString& title) override;
    IMethodBuilder& setDescription(const QString& description) override;
    IMethodBuilder& setParameters(const QString& name, const QJsonObject& parameters, QString* msg = nullptr) override;
    IMethodBuilder& setMethodTask(std::shared_ptr<Task> task) override;
    CapabilityMethod build(QString* msg = nullptr) override;
    void clear() override;

protected:// IMethodBuilder
    bool checkComplete() const override;

public:
    /**
     * @brief Generate schema for tool method
     * @param properties - list of properties for input or output schema
     * @return QJsonObject - JSON object representing the schema
     */
    QJsonObject genSchema(const QVector<Property>& properties) const;

private:
    /**
     * @brief Check if the schema is complete, MUST contains type. If object named properties is not empty, type & description MUST exist.
     * @return bool - true if the schema is complete, false otherwise
     */
    bool checkSchemaComplete(const QJsonObject& schema) const;

private:
    QString mName; 
    QString mTitle; // optional
    QString mDescription;
    QJsonObject mInputSchema;
    QJsonObject mOutputSchema; // optional
    std::shared_ptr<Task> mTask;
};

#endif // TOOLSMETHODBUILDER_H
